#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PDD爬虫配置文件
"""

# PDD相关网址
LOGIN_URL = "https://mms.pinduoduo.com/login/?redirectUrl=https%3A%2F%2Fmms.pinduoduo.com%2F"
GOODS_LIST_URL = "https://mms.pinduoduo.com/goods/goods_list"
API_GOODS_LIST_URL = "https://mms.pinduoduo.com/vodka/v2/mms/query/display/mall/goodsList"

# 浏览器设置
BROWSER_SETTINGS = {
    'headless': False,  # 设为True以无界面运行
    'window_size': (1920, 1080),
    'page_load_timeout': 30,
    'implicit_wait': 10
}

# 爬虫设置
SCRAPER_SETTINGS = {
    'max_pages': 10,  # 最大爬取页数
    'items_per_page': 20,  # 每页商品数量
    'delay_between_requests': 1,  # API请求间隔（秒）
    'login_timeout': 60,  # 登录超时时间（秒）
    'manual_login_timeout': 300  # 手动登录超时（如二维码扫码）
}

# 文件设置
FILE_SETTINGS = {
    'output_file': 'pdd_products.json',
    'log_file': 'pdd_scraper.log',
    'encoding': 'utf-8'
}

# API请求设置
API_SETTINGS = {
    'pre_sale_type': 4,
    'shipment_time_type': 3,
    'is_onsale': 1,
    'sold_out': 0
}

# 请求头配置
DEFAULT_HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36',
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'Content-Type': 'application/json',
    'Origin': 'https://mms.pinduoduo.com',
    'Referer': 'https://mms.pinduoduo.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
}

# Chrome浏览器选项，提高兼容性
CHROME_OPTIONS = [
    '--no-sandbox',
    '--disable-dev-shm-usage',
    '--disable-blink-features=AutomationControlled',
    '--disable-extensions',
    '--disable-plugins',
    '--disable-images',  # 取消注释可加快加载速度
    '--disable-javascript',  # 如果不需要JavaScript可取消注释
]

# 登录页面元素选择器
LOGIN_SELECTORS = {
    'username_input': [
        "input[type='text']",
        "input[placeholder*='用户名']",
        "input[placeholder*='手机']",
        "input[placeholder*='账号']",
        "input[name='username']",
        "input[name='phone']"
    ],
    'password_input': [
        "input[type='password']",
        "input[placeholder*='密码']",
        "input[name='password']"
    ],
    'login_button': [
        "//button[.//span[contains(text(), '登录')]]",  # XPath: button包含span且span文本为登录
        "//button[contains(text(), '登录')]",  # XPath: button直接包含登录文本
        "button[type='submit']",  # CSS: 提交类型按钮
        ".login-btn",  # CSS: 登录按钮class
        ".btn-login",  # CSS: 登录按钮class变体
        "button[class*='login']",  # CSS: class包含login的button
        "button[id*='login']",  # CSS: id包含login的button
        "button[class*='submit']",  # CSS: class包含submit的button
        "input[type='submit']"  # CSS: 提交类型输入框
    ],
    'qr_code': [
        ".qr-code",
        ".qrcode",
        "[class*='qr']",
        "[id*='qr']"
    ]
}

# 登录状态检查选择器
LOGIN_STATUS_SELECTORS = {
    'logout_indicators': [
        ".user-info",
        ".logout",
        ".user-name",
        "[class*='user']",
        "[href*='logout']"
    ],
    'nav_indicators': [
        ".main-nav",
        ".dashboard",
        ".goods-list",
        "[href*='goods']",
        ".sidebar",
        ".nav-menu"
    ]
}

# 商品列表页面选择器
GOODS_LIST_SELECTORS = [
    ".goods-list",
    ".product-list",
    "[class*='goods']",
    "[class*='product']",
    ".table-container",
    ".data-table"
] 